//***********************************************************************************
// Function to apply level 1 rule set

// Author: Aravindh U095232L

//***********************************************************************************
function level_1_rules()
{
	var player_level1, outside = false, building = false;

	for(var z=0; z<level_1.length;z++)
	{
		if(level_1[z].getNPCs().Player)
			if(level_1[z].getNPCs().Player.length!=0)
			{	outside = true;
				player_level1 = level_1[z].getNPCs().Player[0];
				break;
			}	
		else if (level_1[z].getBuildings().length!=0)
		{	
			if(level_1[z].getBuildings()[0].obj.getNPCs().length!=0)
				if(level_1[z].getBuildings()[0].obj.getNPCs().Player)
					if(level_1[z].getBuildings()[0].obj.getNPCs().Player.length!=0)
					{	building =true;
						player_level1 = level_1[z].getBuildings()[0].obj.getNPCs().Player[0];
						conv_type = level_1[z].getType();
						break;
					}
		}

	}


	for(var i=0; i<level_1.length; i++)
	{	
		
		var NPC = level_1[i].getNPCs();
		var army = NPC.Army;
		var rebel = NPC.Rebel;
		var citizen = NPC.Citizen;
		var spy = NPC.Spy;
		var merchant = NPC.Merchant;
		level_1[i].indexer = level_1[i].getIndexOfGrid();
		var obs = level_1[i].getObstacleMap();

		//console.log(army,rebel,citizen,spy,merchant);
		//console.log("TIME "+i+" "+level_1[i].timer);
		if( (level_1[i].done==0) && (level_1[i].timer> 250) )
		{	
			// if(level_1[i].timer==501)
			// alert(i);
			//console.log("inside");
			//console.log(army,rebel,citizen,spy,merchant);

        if (army && rebel)
        {
            if(army.length!=0 && rebel.length!=0)
            {NPC.Rebel[0].meetup = 1;
                NPC.Army[0].meetup = 1;
                level_1[i].end = find_correct_pos(level_1[i],NPC.Army[0]);
                NPC.Rebel[0].setreachDestination(false);
                NPC.Rebel[0].start_event=false;
                level_1[i].done = 4;
                //alert(level_1[i].end.x +" "+ level_1[i].end.y+" "+NPC.Army[0].getPosition().x+" "+NPC.Army[0].getPosition().y);
            }
        }
		else if(merchant && rebel)
		{	
			if(merchant.length!=0 && rebel.length!=0)
			{	NPC.Rebel[0].meetup = 1;
				NPC.Merchant[0].meetup = 1;
				level_1[i].end = find_correct_pos(level_1[i],NPC.Merchant[0]);
				NPC.Rebel[0].setreachDestination(false);
				NPC.Rebel[0].start_event=false;
				level_1[i].done = 1;
				//alert(level_1[i].end.x +" "+ level_1[i].end.y+" "+NPC.Merchant[0].getPosition().x+" "+NPC.Merchant[0].getPosition().y);
			}
		}	
		else if(rebel && spy && citizen)
		{	
			if (Math.random()<0.5)
			{
				if(spy.length!=0 && citizen.length!=0)
				{NPC.Spy[0].meetup = 1;
				NPC.Citizen[0].meetup = 1;
				level_1[i].end = find_correct_pos(level_1[i],NPC.Citizen[0]);
				NPC.Spy[0].setreachDestination(false);
				NPC.Spy[0].start_event=false;
				level_1[i].done= 2;
				//alert(level_1[i].end.x +" "+ level_1[i].end.y+" "+NPC.Citizen[0].getPosition().x+" "+NPC.Citizen[0].getPosition().y);
			}
			}
			else
			{
				if(rebel.length!=0 && citizen.length!=0)
				{NPC.Rebel[0].meetup = 1;
				NPC.Citizen[0].meetup = 1;
				level_1[i].end = find_correct_pos(level_1[i],NPC.Citizen[0]);
				NPC.Rebel[0].setreachDestination(false);
				NPC.Rebel[0].start_event=false;
				level_1[i].done= 3;
				//alert(level_1[i].end.x +" "+ level_1[i].end.y+" "+NPC.Citizen[0].getPosition().x+" "+NPC.Citizen[0].getPosition().y);
				}
			}
		}
		else if	(spy && citizen)
		{	
				if(spy.length!=0 && citizen.length!=0)
				{NPC.Spy[0].meetup = 1;
				NPC.Citizen[0].meetup = 1;
				level_1[i].end = find_correct_pos(level_1[i],NPC.Citizen[0]);
				NPC.Spy[0].setreachDestination(false);
				NPC.Spy[0].start_event=false;
				level_1[i].done = 5;
				//alert(level_1[i].end.x +" "+ level_1[i].end.y+" "+NPC.Citizen[0].getPosition().x+" "+NPC.Citizen[0].getPosition().y);
				}			
		}
		else if(rebel && citizen)
		{	
			if(rebel.length!=0 && citizen.length!=0)
				{NPC.Rebel[0].meetup = 1;
				NPC.Citizen[0].meetup = 1;
				level_1[i].end = find_correct_pos(level_1[i],NPC.Citizen[0]);
				NPC.Rebel[0].setreachDestination(false);
				NPC.Rebel[0].start_event=false;
				level_1[i].done =6;
				//alert(level_1[i].end.x +" "+ level_1[i].end.y+" "+NPC.Citizen[0].getPosition().x+" "+NPC.Citizen[0].getPosition().y);
			}
		}
		else if (citizen)
		{	
			if(citizen.length>=2)
			{NPC.Citizen[0].meetup = 1;
			NPC.Citizen[1].meetup = 1;
			level_1[i].end = find_correct_pos(level_1[i],NPC.Citizen[0]);
			NPC.Citizen[1].setreachDestination(false);
			NPC.Citizen[1].start_event=false;
			level_1[i].done=7;
			//alert(level_1[i].end.x +" "+ level_1[i].end.y+" "+NPC.Citizen[0].getPosition().x+" "+NPC.Citizen[0].getPosition().y);

			}
		} 
			//console.log("DONE " + i + " "+level_1[i].done);
		}


		if(level_1[i].done==1)
			move_to(level_1[i],NPC.Rebel[0],NPC.Merchant[0], level_1[i].indexer, level_1[i].end, obs);
		else if(level_1[i].done==2) 
			move_to(level_1[i],NPC.Spy[0],NPC.Citizen[0], level_1[i].indexer, level_1[i].end, obs);
		else if(level_1[i].done==3) 
			move_to(level_1[i],NPC.Rebel[0],NPC.Citizen[0], level_1[i].indexer, level_1[i].end, obs);
		else if(level_1[i].done==4) 
			move_to(level_1[i],NPC.Rebel[0],NPC.Army[0], level_1[i].indexer, level_1[i].end, obs);
		else if(level_1[i].done==5) 
			move_to(level_1[i],NPC.Spy[0],NPC.Citizen[0], level_1[i].indexer, level_1[i].end, obs);
		else if(level_1[i].done==6) 
			move_to(level_1[i],NPC.Rebel[0],NPC.Citizen[0], level_1[i].indexer, level_1[i].end, obs);
		else if(level_1[i].done==7) 
			move_to(level_1[i],NPC.Citizen[1],NPC.Citizen[0], level_1[i].indexer, level_1[i].end, obs);

		var dk = 0;
		
		if(level_1[i].done==1)
		{	
			if(NPC.Rebel.length!=0)
			if(NPC.Rebel[0].finished)
				{level_1[i].done=0;NPC.Rebel[0].setreachDestination(false);NPC.Rebel[0].start_event=false;NPC.Rebel[0].finished=0;dk=1;}
		}
		else if(level_1[i].done==2)
		{	
			if(NPC.Spy.length!=0)
			if(NPC.Spy[0].finished)
				{level_1[i].done=0;NPC.Spy[0].setreachDestination(false);NPC.Spy[0].start_event=false;NPC.Spy[0].finished=0;dk=1;}
		}	
		else if(level_1[i].done==3)
		{	
		if(NPC.Rebel.length!=0)
			if(NPC.Rebel[0].finished)
				{level_1[i].done=0;NPC.Rebel[0].setreachDestination(false);NPC.Rebel[0].start_event=false;NPC.Rebel[0].finished=0;dk=1;}
		}	
		else if(level_1[i].done==4)
		{	
		if(NPC.Rebel.length!=0)
			if(NPC.Rebel[0].finished)
				{level_1[i].done=0;NPC.Rebel[0].setreachDestination(false);NPC.Rebel[0].start_event=false;NPC.Rebel[0].finished=0;dk=1;}
		}	
		else if(level_1[i].done==5)
		{	
		if(NPC.Spy.length!=0)			
			if(NPC.Spy[0].finished)
				{level_1[i].done=0;NPC.Spy[0].setreachDestination(false);NPC.Spy[0].start_event=false;NPC.Spy[0].finished=0;dk=1;}
		}	
		else if(level_1[i].done==6)
		{	
			if(NPC.Rebel.length!=0)
				if(NPC.Rebel[0].finished)
					{level_1[i].done=0;NPC.Rebel[0].setreachDestination(false);NPC.Rebel[0].start_event=false;NPC.Rebel[0].finished=0;dk=1;}
		}
		else if(level_1[i].done==7)
		{	if(NPC.Citizen.length>=2)
				if(NPC.Citizen[1].finished)
					{level_1[i].done=0;NPC.Citizen[1].setreachDestination(false);NPC.Citizen[1].start_event=false;NPC.Citizen[1].finished=0;dk=1;}
		}

		if(dk)
		{
			level_1[i].timer=0;
		}		

		level_1[i].timer++;

		//console.log("DK "+dk);

		var NPC_building, army_building, rebel_building, citizen_building, spy_building, merchant_building;
		if(level_1[i].getBuildings().length!=0)
		{
		NPC_building = level_1[i].getBuildings()[0].obj.getNPCs();
		army_building = NPC_building.Army;
		rebel_building = NPC_building.Rebel;
		citizen_building = NPC_building.Citizen;
		spy_building = NPC_building.Spy;
		merchant_building = NPC_building.Merchant;
		}

		
		if(army)
			for(var j =0; j <army.length;j++)
			{	
				if(level_1[i].done==4 && j==0)
					continue;
				else if(outside)
				{
					var inside = false;
					inside = player_level1.inRadius(army[j]);

					if(inside)
					{
					army[j].player_timer++;
					if(army[j].player_timer>150)
						{inside = false;}	
					}

					if(inside == false)
					{	army[j].randomMove();
						army[j].player_timer =0;
					}
					else
					{
						var dir = check_dir(army[j],player_level1);
						if(dir)
	        					army[j].changeFacing(dir);
					}
				}
				else
					army[j].randomMove();
			}
			
		if(rebel)
			for(var k =0; k <rebel.length;k++)
			{
				if( (level_1[i].done==1 || level_1[i].done==3 || level_1[i].done==4 || level_1[i].done==6) && k==0 )
					continue;
				else if(outside)
				{
					var inside = false;
					inside = player_level1.inRadius(rebel[k]);

					if(inside)
					{
					rebel[k].player_timer++;
					if(rebel[k].player_timer>150)
						{inside = false;}	
					}

					if(inside == false)
					{
						rebel[k].randomMove();
						rebel[k].player_timer = 0;
					}
					else
					{
						var dir = check_dir(rebel[k],player_level1);
						if(dir)
	        					rebel[k].changeFacing(dir);
					}
				}
				else
					rebel[k].randomMove();
			}
	
		if(citizen)
			for(var l =0; l <citizen.length;l++)
			{
				if( (level_1[i].done==2 || level_1[i].done==3 || level_1[i].done==5 || level_1[i].done==6) && l==0 )
					continue;
				else if(level_1[i].done==7 && (l==0||l==1))
					continue;
				else if(outside)
				{
					var inside = false;
					inside = player_level1.inRadius(citizen[l]);
					if(inside)
					{
					citizen[l].player_timer++;
					if(citizen[l].player_timer>150)
						{inside = false;}	
					}
					
					if(inside == false)
					{	citizen[l].player_timer=0;
						citizen[l].randomMove();
					}
					else
					{
						var dir = check_dir(citizen[l],player_level1);
						if(dir)
	        					citizen[l].changeFacing(dir);
					}
				}
				else
					citizen[l].randomMove();
			}

		if(spy)
			for(var m =0; m <spy.length;m++)
			{
				if( (level_1[i].done==2 || level_1[i].done==5) && m==0 )
					continue;
				else if(outside)
				{
					var inside = false;
					inside = player_level1.inRadius(spy[m]);

					if(inside)
					{
					spy[m].player_timer++;
					if(spy[m].player_timer>150)
						{inside = false;}	
					}

					if(inside == false)
					{	spy[m].randomMove();
						spy[m].player_timer=0;
					}
					else
					{
						var dir = check_dir(spy[m],player_level1);
						if(dir)
	        					spy[m].changeFacing(dir);
					}
				}
				else
					spy[m].randomMove();
			}

		if(merchant)
			for(var n =0; n <merchant.length;n++)
			{
				if( (level_1[i].done==1) && n==0 )
					continue;
				else if(outside)
				{
					var inside = false;
					inside = player_level1.inRadius(merchant[n]);

					if(inside)
					{
					merchant[n].player_timer++;
					if(merchant[n].player_timer>150)
						{inside = false;}	
					}

					if(inside == false)
					{
						merchant[n].randomMove();
						merchant[n].player_timer =0;
					}
					else
					{
						var dir = check_dir(merchant[n],player_level1);
						if(dir)
	        					merchant[n].changeFacing(dir);
					}
				}
				else
					merchant[n].randomMove();
			}

		//console.log (player_level1, army_building, rebel_building, spy_building , merchant_building ,citizen_building);


		if(army_building)
			for(var j =0; j <army_building.length;j++)
			{	
				// if(outside==false)
				// {
				// 	var inside = false;
				// 	inside = player_level1.inRadius(army_building[j]);
				// 	if(inside == false)
				// 		army_building[j].randomMove();
				// 	else
				// 	{	
				// 		var dir = check_dir(army_building[j],player_level1);
				// 		if(dir)
	   //     					army_building[j].changeFacing(dir);
				// 	}
				// }
				// else
					army_building[j].randomMove();
			}


		if(rebel_building)	
			for(var k =0; k <rebel_building.length;k++)
			{	
				// if(outside==false)
				// {
				// 	var inside = false;
				// 	inside = player_level1.inRadius(rebel_building[k]);
				// 	if(inside == false)
				// 		rebel_building[k].randomMove();
				// 	else
				// 	{	
				// 		var dir = check_dir(rebel_building[k],player_level1);
				// 		if(dir)
	   //     					rebel_building[k].changeFacing(dir);
				// 	}	
				// }
				//else
					rebel_building[k].randomMove();	
			}

		if(citizen_building)	
			for(var l =0; l <citizen_building.length;l++)
			{	
				// if(outside==false)
				// {
				// 	var inside = false;
				// 	inside = player_level1.inRadius(citizen_building[l]);
				// 	if(inside == false)
				// 		citizen_building[l].randomMove();
				// 	else
				// 	{	
				// 		var dir = check_dir(citizen_building[l],player_level1);
				// 		if(dir)
	   //     					citizen_building[l].changeFacing(dir);
				// 	}
				// }
				//else
					citizen_building[l].randomMove();	
			}

		if(spy_building)
			for(var m =0; m <spy_building.length;m++)
			{	
				// if(outside==false)
				// {
				// 	var inside = false;
				// 	inside = player_level1.inRadius(spy_building[m]);
				// 	if(inside == false)
				// 		spy_building[m].randomMove();
				// 	else
				// 	{	
				// 		var dir = check_dir(spy_building[m],player_level1);
				// 		if(dir)
	   //     					spy_building[m].changeFacing(dir);
				// 	}
				// }	
				//else
					spy_building[m].randomMove();
			}	

		if(merchant_building)
			for(var n =0; n <merchant_building.length;n++)
			{	
				// if(outside==false)
				// {
				// 	var inside = false;
				// 	inside = player_level1.inRadius(merchant_building[n]);
				// 	if(inside == false)
				// 		merchant_building[n].randomMove();
				// 	else
				// 	{	
				// 		var dir = check_dir(merchant_building[n],player_level1);
				// 		if(dir)
	   //     					merchant_building[n].changeFacing(dir);
				// 	}	
				// }
				//else
					merchant_building[n].randomMove();
			}	

	}


for(var u =0; u<level_temp.length; u++)
	{	
		var NPC = level_temp[u].getNPCs();
		var army = NPC.Army;
		var rebel = NPC.Rebel;
		var citizen = NPC.Citizen;
		var spy = NPC.Spy;
		var merchant = NPC.Merchant;

		var NPC_building, army_building, rebel_building, citizen_building, spy_building, merchant_building;
		
		if(level_temp[u].getBuildings().length!=0)
		{
		NPC_building = level_temp[u].getBuildings()[0].obj.getNPCs();
		army_building = NPC_building.Army;
		rebel_building = NPC_building.Rebel;
		citizen_building = NPC_building.Citizen;
		spy_building = NPC_building.Spy;
		merchant_building = NPC_building.Merchant;
		}

		//console.log(i ,level_temp[i].getNPCs());

		if(army)
			for(var j =0; j <army.length;j++)
			{	
				army[j].randomMove();
			}
		if(rebel)	
			for(var k =0; k <rebel.length;k++)
				rebel[k].randomMove();

		if(citizen)	
			for(var l =0; l <citizen.length;l++)
				citizen[l].randomMove();

		if(spy)
			for(var m =0; m <spy.length;m++)
				spy[m].randomMove();

		if(merchant)
			for(var n =0; n <merchant.length;n++)
				merchant[n].randomMove();

		if(army_building)
			for(var j =0; j <army_building.length;j++)
				army_building[j].randomMove();

		if(rebel_building)	
			for(var k =0; k <rebel_building.length;k++)
				rebel_building[k].randomMove();

		if(citizen_building)	
			for(var l =0; l <citizen_building.length;l++)
				citizen_building[l].randomMove();

		if(spy_building)
			for(var m =0; m <spy_building.length;m++)
				spy_building[m].randomMove();

		if(merchant_building)
			for(var n =0; n <merchant_building.length;n++)
				merchant_building[n].randomMove();
	}
}
